//
//  MyDownloadViewController.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/11.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "MyDownloadViewController.h"
#import "MyDownLoadTableViewCell.h"
#import "LZYFileTool.h"

@interface MyDownloadViewController ()<UIDocumentInteractionControllerDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *DownLoadList;
@property (nonatomic, strong) UIDocumentInteractionController *documentController;
@end

@implementation MyDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNewStatusesMessage:self.DownLoadList.count];
}

- (NSArray *)DownLoadList{
    if(!_DownLoadList){
        _DownLoadList = [DownLoadModel arrayOfModelsFromDictionaries:[LZYModelOperationTool queryModelsWithSql:getDownLoadFileSql withUserID:[[User shareInstance] getUserId]] error:nil];
    }
    return _DownLoadList;
}

- (void)loaddata{
    [self.tableView reloadData];
}

- (NSString *)setNavTitle{
    return @"我的下载";
}

- (UITableView *)set_tableViewWithFrame{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_View_Height)];
    self.tableView = tableView;
    return self.tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.tableView showBlankPageViewWithCount:self.DownLoadList reloadAction:@selector(loaddata) WithTarget:self];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyDownLoadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyDownLoadTableViewCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"MyDownLoadTableViewCell" owner:self options:nil][indexPath.section];
        cell.selectionStyle = UIAccessibilityTraitNone;
    }
    [cell setdataWithDownLoadModel:self.DownLoadList[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self openFileWithModel:self.DownLoadList[indexPath.row]];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        DownLoadModel *downLoadModel = self.DownLoadList[indexPath.row];
        [LZYModelOperationTool deleteModel:downLoadModel withUserID:[[User shareInstance] getUserId]];
        //删除缓存文件
        [LZYFileTool removeFileAtPath:downLoadModel.cachePath];
        [self.DownLoadList removeObject:self.DownLoadList[indexPath.row]];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    return @[deleteRoWAction];
}

- (void)openFileWithModel:(DownLoadModel *)downLoadModel{
    NSURL *url = [NSURL fileURLWithPath:downLoadModel.cachePath];
    self.documentController =[UIDocumentInteractionController interactionControllerWithURL:url];
    self.documentController.delegate = self;
    [self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self;
}

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
    return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
    return  self.view.frame;
}

@end
